package javaweb.tail;

import bean.Tail;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import javaweb.tail.util.DBUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/tail/list","/tail/add","/tail/delete","/tail/detail","/tail/edit"})
public class TailServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        HttpSession session = request.getSession(false);
        if(session!=null&&session.getAttribute("username")!=null){
            if("/tail/list".equals(servletPath)){
                doList(request,response);
            }else if("/tail/add".equals(servletPath)){
                doAdd(request,response);
            }else if("/tail/delete".equals(servletPath)){
                doDelete(request,response);
            }else if("/tail/detail".equals(servletPath)){
                doDetail(request,response);
            }else if("/tail/edit".equals(servletPath)){
                doEdit(request,response);
            }
        }

    }



    private void doEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String flag = request.getParameter("flag");
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try{
            conn = DBUtil.getConnection();
            String sql = "update tail set name=?,password=?,address=?,phone=? where name=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,address);
            ps.setString(4,phone);
            ps.setString(5,flag);
            count = ps.executeUpdate();

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath() + "/tail/list");
        }else{
            response.sendRedirect(request.getContextPath() + "/editerror.jsp");
        }

    }

    private void doDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String username = request.getParameter("username");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Tail tail = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select password,phone,address from tail where name=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            if(rs.next()){
                String password = rs.getString("password");
                String address = rs.getString("address");
                String phone = rs.getString("phone");
                tail = new Tail(username,password,phone,address);
            }

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        request.setAttribute("tail",tail);
        request.getRequestDispatcher("/edit.jsp").forward(request,response);
    }

    public void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String username = request.getParameter("username");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try{
            conn = DBUtil.getConnection();
            String sql = "delete from tail where name=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            count = ps.executeUpdate();

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath() + "/tail/list");
        }
    }


    private void doAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try{
            conn = DBUtil.getConnection();
            String sql = "insert into tail(name,password,phone,address) values(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,phone);
            ps.setString(4,address);
            count = ps.executeUpdate();

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath()+"/tail/list");
        }else{
            response.sendRedirect(request.getContextPath() + "/adderror.jsp");
        }

    }

    private void doList(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 建立一个容器装数据
        List<Tail> tails = new ArrayList<>();
        try{
            conn = DBUtil.getConnection();
            String sql = "select name,password,phone,address from tail";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String username = rs.getString("name");
                String password = rs.getString("password");
                String phone = rs.getString("phone");
                String address = rs.getString("address");
                tails.add(new Tail(username,password,phone,address));
            }

        }catch(SQLException e){
            e.getClass().getName();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        request.setAttribute("tails",tails);
        request.getRequestDispatcher("/tail.jsp").forward(request,response);
    }
}
